#include "saveform.h"
#include "ui_saveform.h"

#include <QDir>
#include <QDesktopServices>
#include <QUrl>
#include <QMessageBox>

SaveForm::SaveForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SaveForm)
{
    ui->setupUi(this);
    initCheckBoxes();
}

SaveForm::~SaveForm()
{
    delete ui;
}


void SaveForm::on_Bt_confirm_clicked()
{
    this->hide();
}


void SaveForm::on_Bt_look_clicked()
{
    openFolder();
}


void SaveForm::openFolder() {
    QString appDir = QCoreApplication::applicationDirPath();
    QString imgPath = appDir + "/Pic";
    imgPath = QDir::toNativeSeparators(imgPath);

    if (QDir(imgPath).exists()) {
        QDesktopServices::openUrl(QUrl::fromLocalFile(imgPath));
    }
    else {
        QMessageBox::warning(this, "Error", "Folder not found.");
    }
}

void SaveForm::initCheckBoxes() {
    for (int group = 1; group < 7; group++) {
        for (int param = 1; param < 5;param++) {
            QString objName = QString("cBox_%1_%2").arg(group).arg(param);
            QCheckBox* cb = findChild<QCheckBox*>(objName);
            if (cb) {
                cb->setProperty("groupIndex", group);
                cb->setProperty("paramIndex", param);
                connect(cb, &QCheckBox::stateChanged, this, &SaveForm::onCheckBoxStateChanged);
            }
        }
    }
}

void SaveForm::onCheckBoxStateChanged(int) {
    QCheckBox* cb = qobject_cast<QCheckBox*>(sender());
    if (!cb) return;

    // 获取属性值
    int group = cb->property("groupIndex").toInt();
    int param = cb->property("paramIndex").toInt();

    // 获取对应的结构体和参数成员
    CameraSw& cameraSw = m_saveSw.getCameraSw(group);
    int value = cb->isChecked() ? 1 : 0;
    cameraSw.*cameraSwMembers[param-1] = value; // 使用成员指针赋值
    emit sigSaveState(group);
}
